Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports System.Diagnostics
Imports System.Drawing.Imaging
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.IO


Public Class CaptureWebPage
    Private Const EXTRACTIMAGE_EXE As String = "IECapt.exe"
    Private Const TIMEOUT As Integer = 120000
    Private TMP_NAME As String = "Temp\"


    Public Sub New()
    End Sub

    Private Sub Shot(ByVal url As String, ByVal rootDir As String)
        Dim p As New Process()
        p.StartInfo.FileName = rootDir & EXTRACTIMAGE_EXE
        p.StartInfo.Arguments = [String].Format("""{0}"" ""{1}""", "--url=" & url, "--out=" & rootDir & TMP_NAME)

        'p.StartInfo.UseShellExecute = true; 
        'p.StartInfo.CreateNoWindow = true;  
        p.Start()
        p.WaitForExit()
        p.Dispose()
    End Sub

    Private Function Scale(ByVal imgPhoto As System.Drawing.Image, ByVal Width As Integer, ByVal Height As Integer) As System.Drawing.Image
        Dim srcWidth As Integer = imgPhoto.Width

        Dim srcHeight As Integer = imgPhoto.Height
        Dim srcX As Integer = 0
        Dim srcY As Integer = 0
        Dim destX As Integer = 0
        Dim destY As Integer = 0
        Dim percent As Single = 0
        Dim percentWidth As Single = 0
        Dim percentHeight As Single = 0
        percentWidth = (CSng(Width) / CSng(srcWidth))
        percentHeight = (CSng(Height) / CSng(srcHeight))
        If percentHeight < percentWidth Then
            percent = percentWidth
            destY = 0
        Else
            percent = percentHeight
            destX = 0
        End If
        Dim destWidth As Integer = CInt((srcWidth * percent))
        Dim destHeight As Integer = CInt((srcHeight * percent))
        Dim bmPhoto As New System.Drawing.Bitmap(Width, Height, PixelFormat.Format24bppRgb)
        bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution)
        Dim grPhoto As Graphics = Graphics.FromImage(bmPhoto)
        grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic
        grPhoto.DrawImage(imgPhoto, New Rectangle(destX, destY, destWidth, destHeight), New Rectangle(srcX, srcY, srcWidth, srcHeight), GraphicsUnit.Pixel)
        grPhoto.Dispose()
        Return bmPhoto
    End Function

    Public Function GetImage(ByVal url As String, ByVal name As String, ByVal rootDir As String, ByVal width As Integer, ByVal height As Integer) As String
        TMP_NAME += name & ".png"
        Dim fName As String = rootDir & TMP_NAME
        Shot(url, rootDir)
        Dim snapshotImage As System.Drawing.Image = System.Drawing.Image.FromFile(fName)
        fName = rootDir & "OutPut" & "\" & name & ".png"
        If File.Exists(fName) Then
            File.Delete(fName)
        End If
        snapshotImage.Save(fName, ImageFormat.Png)
        Return name & ".png"
    End Function
End Class
